package com.jhgsys.internal.incentive.service.impl;

import com.jhgsys.internal.incentive.entity.IncentiveModuleConfig;
import com.jhgsys.internal.incentive.mapper.IncentiveModuleConfigMapper;
import com.jhgsys.internal.incentive.service.IIncentiveModuleConfigService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.peccancy.entity.PeccancyModuleConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author yangjie
 * @date 2024-09-10 16:29:59
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IncentiveModuleConfigServiceImpl extends ServiceImpl<IncentiveModuleConfigMapper, IncentiveModuleConfig> implements IIncentiveModuleConfigService {

    /**
     * 查询（分页）
     * @author yangjie
     * @date 2024-09-10 16:29:59
     * @param request QueryRequest
     * @param incentiveModuleConfig incentiveModuleConfig
     * @return IPage<IncentiveModuleConfig>
    */
    @Override
    public IPage<IncentiveModuleConfig> findIncentiveModuleConfigPage(QueryRequest request, IncentiveModuleConfig incentiveModuleConfig) {
        LambdaQueryWrapper<IncentiveModuleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveModuleConfig::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<IncentiveModuleConfig> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author yangjie
     * @date 2024-09-10 16:29:59
     * @param incentiveModuleConfig incentiveModuleConfig
     * @return List<IncentiveModuleConfig>
     */
    @Override
    public List<IncentiveModuleConfig> findIncentiveModuleConfigList(IncentiveModuleConfig incentiveModuleConfig) {
	    LambdaQueryWrapper<IncentiveModuleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveModuleConfig::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author yangjie
    * @date 2024-09-10 16:29:59
    * @param id
    * @return IncentiveModuleConfig
    */
    @Override
    public IncentiveModuleConfig findIncentiveModuleConfigById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author yangjie
     * @date 2024-09-10 16:29:59
     * @param incentiveModuleConfig incentiveModuleConfig
     */
    @Override
    @Transactional
    public void createIncentiveModuleConfig(IncentiveModuleConfig incentiveModuleConfig) {
        incentiveModuleConfig.setCreatedTime(new Date());
        this.save(incentiveModuleConfig);
    }

    /**
     * 修改
     * @author yangjie
     * @date 2024-09-10 16:29:59
     * @param incentiveModuleConfig incentiveModuleConfig
     */
    @Override
    @Transactional
    public void updateIncentiveModuleConfig(IncentiveModuleConfig incentiveModuleConfig) {
        incentiveModuleConfig.setUpdatedTime(new Date());
        this.saveOrUpdate(incentiveModuleConfig);
    }

    /**
     * 逻辑删除
     * @author yangjie
     * @date 2024-09-10 16:29:59
     * @param incentiveModuleConfig incentiveModuleConfig
     * @param incentiveModuleConfigIds incentiveModuleConfigIds
     */
    @Override
    @Transactional
    public void deleteIncentiveModuleConfig(IncentiveModuleConfig incentiveModuleConfig,String incentiveModuleConfigIds) {
        List<String> list = Arrays.asList(incentiveModuleConfigIds.split(StringPool.COMMA));
        this.baseMapper.update(incentiveModuleConfig,new LambdaQueryWrapper<IncentiveModuleConfig>().in(IncentiveModuleConfig::getModuleConfigId,list));
	}

    /**
    * 物理删除
    * @author yangjie
    * @date 2024-09-10 16:29:59
    * @param incentiveModuleConfigIds incentiveModuleConfigIds
    */
    @Override
    @Transactional
    public void physicallyDeleteIncentiveModuleConfig(String incentiveModuleConfigIds) {
        List<String> list = Arrays.asList(incentiveModuleConfigIds.split(StringPool.COMMA));
        LambdaQueryWrapper<IncentiveModuleConfig> wapper = new LambdaQueryWrapper<>();
        wapper.in(IncentiveModuleConfig::getModuleConfigId,list);
        this.remove(wapper);
    }

    /**
     * 根据所属公司查询
     * @param tenantId
     * @return
     */
    @Override
    public IncentiveModuleConfig findIncentiveModuleConfigByTenantId(String tenantId){
        LambdaQueryWrapper<IncentiveModuleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveModuleConfig::getDeletedFlg, 0);
        queryWrapper.eq(IncentiveModuleConfig::getTenantId, tenantId);
        queryWrapper.last(" LIMIT 1 ");
        return this.baseMapper.selectOne(queryWrapper);
    }
}
